import { defineStore } from "pinia";
import {
  userLoginApi,
  getUserInfoById,
  getUserMenusByRoleId,
} from "@/api/login/login";
import type { ILogin } from "@/types";
import { localCache } from "@/utils/cache";
import router from "@/router";
import { LOGIN_TOKEN } from "@/global/constants";
import { mapMenusToRoutes } from "@/utils/map-menus";
import useMainStore from "@/store/main/main";

interface ILoginState {
  token: string;
  userInfo: any;
  userMenus: any;
}

const useLoginStore = defineStore("login", {
  // 如何制定state的类型
  state: (): ILoginState => ({
    token: localCache.getCache(LOGIN_TOKEN) ?? "",
    userInfo: localCache.getCache("userInfo") ?? {},
    userMenus: localCache.getCache("userMenus") ?? [],
  }),
  actions: {
    async loginAccountAction(account: ILogin) {
      // 1.账号登录, 获取token等信息
      const loginResult = await userLoginApi(account);
      const id = loginResult.data.id;
      this.token = loginResult.data.token;
      localCache.setCache(LOGIN_TOKEN, this.token);

      // 2.获取登录用户的详细信息(role信息)
      const userInfoResult = await getUserInfoById(id);
      const userInfo = userInfoResult.data;
      this.userInfo = userInfo;

      // 3.根据角色请求用户的权限(菜单menus)
      const userMenusResult = await getUserMenusByRoleId(this.userInfo.role.id);
      const userMenus = userMenusResult.data;
      this.userMenus = userMenus;

      // 4.进行本地缓存
      localCache.setCache("userInfo", userInfo);
      localCache.setCache("userMenus", userMenus);

      // 5.请求所有roles/departments的数据
      const mainStore = useMainStore();
      mainStore.fetchEntireDataAction();

      // 动态添加路由
      const routes = mapMenusToRoutes(userMenus);
      routes.forEach((route) => router.addRoute("main", route));

      // 5.页面跳转(main页面)
      router.push("/main");
    },
    loadLocalCacheAction() {
      // 1.用户进行刷新默认加载数据
      const token = localCache.getCache(LOGIN_TOKEN);
      const userInfo = localCache.getCache("userInfo");
      const userMenus = localCache.getCache("userMenus");
      if (token && userInfo && userMenus) {
        this.token = token;
        this.userInfo = userInfo;
        this.userMenus = userMenus;

        //1.请求所有roles/departments的数据
        const mainStore = useMainStore();
        mainStore.fetchEntireDataAction();

        // 2.动态添加路由
        const routes = mapMenusToRoutes(userMenus);
        routes.forEach((route) => router.addRoute("main", route));
      }
    },
  },
});

export default useLoginStore;
